package com.fqgj.sentry.biz.mc;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.manage.entity.McApplicationEntity;
import com.fqgj.sentry.manage.entity.McMerchantEntity;
import com.fqgj.sentry.manage.service.ApplicationService;
import com.fqgj.sentry.controller.mc.request.ApplicationVo;
import com.fqgj.sentry.biz.mc.vo.application.MerchantVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by liupengpeng on 2017/7/14.
 */
@Service
public class ApplicationBusiness {

    @Autowired
    private ApplicationService applicationService;


    /**
     * 根据商户Id查询相应的商户
     *
     * @param merchantId
     * @return
     */
    public MerchantVo selectByMerchantId(Long merchantId) {
        MerchantVo merchantVo = new MerchantVo();
        McMerchantEntity merchantEntity = applicationService.selectByMerchantId(merchantId);
        BeanUtils.copyProperties(merchantEntity, merchantVo);
        return merchantVo;
    }

    /**
     * 查询集合
     *
     * @param merchantId
     * @param apiPage
     * @return
     */
    public ApiPage selectListForPage(Long merchantId, ApiPage apiPage) {
        List<ApplicationVo> applicationVoList = new ArrayList<>();
        List<McApplicationEntity> entityList = applicationService.selectListForPage(merchantId, apiPage);
        if (CollectionUtils.isNotEmpty(entityList)) {
            for (McApplicationEntity mcApplicationEntity : entityList) {
                ApplicationVo temp = new ApplicationVo();
                BeanUtils.copyProperties(mcApplicationEntity, temp);
                applicationVoList.add(temp);
            }
        }
        apiPage.setData(applicationVoList);
        return apiPage;
    }

    /**
     * 删除
     *
     * @param applicationId
     * @return
     */
    public Integer deleteById(Long applicationId) {
        return applicationService.deleteById(applicationId);
    }

    /**
     * 新增
     *
     * @param applicationVo
     * @return
     */
    public Integer insert(ApplicationVo applicationVo) {
        McApplicationEntity entity = new McApplicationEntity();
        BeanUtils.copyProperties(applicationVo, entity);
        return applicationService.insert(entity);
    }

    /**
     * 修改
     *
     * @param applicationVo
     * @return
     */
    public Integer update(ApplicationVo applicationVo) {
        McApplicationEntity entity = new McApplicationEntity();
        BeanUtils.copyProperties(applicationVo, entity);
        return applicationService.update(entity);
    }


}
